package org.jeecg.modules.sggg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.sggg.entity.SgTemplateFive;
import org.jeecg.modules.sggg.entity.SgTemplateItem;
import org.jeecg.modules.sggg.service.ISgTemplateFiveService;
import org.jeecg.modules.sggg.service.ISgTemplateItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 五辆马车模板
 * @Author: jeecg-boot
 * @Date: 2022-10-26
 * @Version: V1.0
 */
@Api(tags = "五辆马车模板")
@RestController
@RequestMapping("/sggg/sgTemplateFive")
@Slf4j
public class SgTemplateFiveController extends JeecgController<SgTemplateFive, ISgTemplateFiveService> {
    @Autowired
    private ISgTemplateFiveService sgTemplateFiveService;

    @Autowired
    private ISgTemplateItemService sgTemplateItemService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 分页列表查询
     *
     * @return 结果
     */
    @ApiOperation(value = "五辆马车模板-分页列表查询", notes = "五辆马车模板-分页列表查询")
    @GetMapping(value = "/list")
    public Result<List<SgTemplateFive>> queryPageList(SgTemplateFive five) {
        List<SgTemplateFive> pageList = sgTemplateFiveService.queryList(five);
        return Result.OK(pageList);
    }

    @AutoLog(value = "五辆马车模板-添加")
    @ApiOperation(value = "五辆马车模板-添加", notes = "五辆马车模板-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SgTemplateFive sgTemplateFive) {
        sgTemplateFiveService.save(sgTemplateFive);
        sgTemplateFive.getItemList().forEach(item -> {
            item.setFiveId(sgTemplateFive.getId());
            sgTemplateItemService.save(item);
        });

        this.redisHelp(sgTemplateFive);

        return Result.OK("添加成功！");
    }

    @AutoLog(value = "五辆马车模板-编辑")
    @ApiOperation(value = "五辆马车模板-编辑", notes = "五辆马车模板-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SgTemplateFive sgTemplateFive) {
        sgTemplateItemService.remove(
                new QueryWrapper<SgTemplateItem>().eq("five_id", sgTemplateFive.getId())
        );
        sgTemplateFiveService.updateById(sgTemplateFive);
        sgTemplateFive.getItemList().forEach(item -> {
            item.setFiveId(sgTemplateFive.getId());
            sgTemplateItemService.save(item);
        });

        this.redisHelp(sgTemplateFive);

        return Result.OK("编辑成功!");
    }


    @AutoLog(value = "五辆马车模板-通过id删除")
    @ApiOperation(value = "五辆马车模板-通过id删除", notes = "五辆马车模板-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        SgTemplateFive sgTemplateFive = sgTemplateFiveService.getById(id);

        sgTemplateFiveService.removeById(id);
        sgTemplateItemService.remove(new QueryWrapper<SgTemplateItem>().eq("five_id", id));

        this.redisHelp(sgTemplateFive);

        return Result.OK("删除成功!");
    }

    @AutoLog(value = "五辆马车模板-批量删除")
    @ApiOperation(value = "五辆马车模板-批量删除", notes = "五辆马车模板-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.sgTemplateFiveService.removeByIds(Arrays.asList(ids.split(",")));

        return Result.OK("批量删除成功!");
    }

    @ApiOperation(value = "五辆马车模板-通过id查询", notes = "五辆马车模板-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SgTemplateFive> queryById(@RequestParam(name = "id") String id) {
        SgTemplateFive sgTemplateFive = sgTemplateFiveService.getById(id);
        if (sgTemplateFive == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgTemplateFive);
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SgTemplateFive sgTemplateFive) {
        return super.exportXls(request, sgTemplateFive, SgTemplateFive.class, "五辆马车模板");
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SgTemplateFive.class);
    }


    /**
     * 同步数据到redis
     */
    @Async
    public void redisHelp(SgTemplateFive sgTemplateFive) {
        //根据新添加模板类型查找所有相同类型的模板项
        List<SgTemplateItem> items = sgTemplateItemService.findByType(sgTemplateFive.getType());
        List<Map<String, Object>> list = new ArrayList<>();
        for (SgTemplateItem item : items) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", item.getName());

            Map<String, Object> valueMap = new HashMap<>();
            valueMap.put("name", item.getName());
            valueMap.put("mode", item.getMode());
            valueMap.put("remark", item.getRemark());

            map.put("value", valueMap);

            list.add(map);
        }
        //存储redis
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        ops.set("template" + sgTemplateFive.getType(), list);
    }


    /**
     * 通过type查询redis模板项
     */
    @ApiOperation(value = "五辆马车模板-通过type查询模板项", notes = "五辆马车模板-通过type查询模板项")
    @GetMapping(value = "/queryByType")
    public Result<Object> queryByType(String type) {

        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        Object o = ops.get("template" + type);

        return Result.OK(o);
    }

    /**
     * 获取健康指导类型
     *
     * @return
     */
    @GetMapping("/getHealthyItems")
    public Result<List<SgTemplateItem>> getHealthyItems(String item) {
        SgTemplateFive sgTemplateFives = sgTemplateFiveService.getOne(new QueryWrapper<SgTemplateFive>().eq("type", item));
        if (sgTemplateFives != null) {
            List<SgTemplateItem> sgTemplateItems = sgTemplateItemService.list(new QueryWrapper<SgTemplateItem>().eq("five_id", sgTemplateFives.getId()));
            return Result.ok(sgTemplateItems);
        }
        return Result.error("没有数据");
    }
}
